/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */


/**
 * 广度优先遍历
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth1 = function (root) {
  if (!root) return 0
  let queue = []
  let res = 0
  queue.push(root)
  while (queue.length) {
    let size = queue.length
    while (size > 0) {
      let node = queue.shift()
      if (node.left) {
        queue.push(node.left)
      }
      if (node.right) {
        queue.push(node.right)
      }
      size--
    }
    res++
  }
  return res
};

/**
 * 深度优先遍历
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth2 = function (root) {
  if (!root) return 0
  let leftDepth = maxDepth2(root.left)
  let rightDepth = maxDepth2(root.right)
  return 1 + Math.max(leftDepth, rightDepth)
}
